package master.project.web.global;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class SessionBindSuccessHandler implements AuthenticationSuccessHandler {
	
	private static Log log = LogFactory.getLog(SessionBindSuccessHandler.class);
	
	private String defaultTargetURL;

	public SessionBindSuccessHandler(String defaultTargetURL) {
		this.defaultTargetURL = defaultTargetURL;
	}
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
		try{
//		    HttpSession session = request.getSession();
//		    UserDetails userDetails = (UserDetails) authentication.getPrincipal();
//		    User user = userMgmtManager.getUserByUsername(userDetails.getUsername());
//		    
//		    Collection<GrantedAuthority> roleList = userDetails.getAuthorities();
//		    String[] roles = new String[roleList.size()];
//		    int i=0;
//		    for(GrantedAuthority role : roleList) {
//		    	roles[i] = role.getAuthority();
//		    	i++;
//		    }
//		    user.setRoles(roles);	    
//		    
//		    UserJoinJob userJob = userMgmtManager.getUserJobByUserPk(user.getPk());
//		    if(userJob != null) {
//		    	Organization org = orgMgmtManager.getOrgByPk(userJob.getOrgID());
//		    	if(org != null)
//		    		session.setAttribute("currentUserOrg", org);
//		    }
//			
//		    session.setAttribute("currentUser", user);
		} catch(Exception err) {
			log.error("用户信息读取失败。", err);
		}
		response.sendRedirect(defaultTargetURL);
	}
}
